<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Product;
use App\Models\Category;
use App\Models\Label;
use App\Models\ProductSku;
use App\Support\Image;
use App\Support\Setting;
use App\Support\Word;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class ProductController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        /*$image = app(Image::class)->sliceImage(public_path('t.jpg'),4);
        dd($image);*/
        /*app(Word::class)->he(public_path('mysql.docx'),public_path('t.jpg'));
        dd(13);*/

        /*$data = '天秤座-太空灰,天秤座-白色,天秤座-電鍍, 天蠍座-太空灰,天蠍座-白色,天蠍座-電鍍, 射手座-太空灰,射手座-白色,射手座-電鍍, 巨蟹座-太空灰,巨蟹座-白色,巨蟹座-電鍍, 摩羯座-太空灰,摩羯座-白色,摩羯座-電鍍, 水瓶座-太空灰,水瓶座-白色,水瓶座-電鍍, 獅子座-太空灰,獅子座-白色,獅子座-電鍍, 白羊座-太空灰,白羊座-白色,白羊座-電鍍, 處女座-太空灰,處女座-白色,處女座-電鍍, 金牛座-太空灰,金牛座-白色,金牛座-電鍍, 雙子座-太空灰,雙子座-白色,雙子座-電鍍, 雙魚座-太空灰,雙魚座-白色,雙魚座-電鍍';
        $data = explode(',',$data);
        foreach ($data as $item){
            ProductSku::create([
                'product_id'=>11,
                'name'=>trim($item),
                'price'=>400,
                'stock'=>9999,
                'status'=>1
            ]);
        }*/

        return Grid::make(Product::with('category'), function (Grid $grid) {
            $grid->model()->orderBy('sort');
            $grid->column('name');
            $grid->column('img')->image('',50,50);
            $grid->column('price');
            $grid->column('sales');
            $grid->column('stock');
            $grid->column('status')->switch();
            $grid->column('sort')->orderable();
            $grid->column('created_at');
            $grid->quickSearch(['name']);
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Product(), function (Show $show) {
            $show->field('id');
            $show->field('category_id');
            $show->field('name');
            $show->field('sub_name');
            $show->field('slogan');
            $show->field('img');
            $show->field('price');
            $show->field('market_price');
            $show->field('sales');
            $show->field('sales_fake');
            $show->field('stock');
            $show->field('sort');
            $show->field('picture');
            $show->field('describe');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(Product::with(['labels','attribute','category','skus']), function (Form $form) {
            $form->tab('基本信息', function (Form $form) {
                $form->tree('category','分類')
                    ->nodes(Category::get()->toArray())
                    ->customFormat(function ($v) { // 格式化外部注入的值
                        if (!$v) return [];
                        return array_column($v, 'id');
                    })->required();
                $form->text('name')->required();
                $form->text('sub_name');
                $form->textarea('slogan');
                $form->image('img')->autoUpload()->uniqueName()->move('product')->required();
                $form->multipleImage('picture')->autoUpload()->uniqueName()->move('product')->limit(20)->saving(function ($paths) {
                    return implode(',', $paths);
                });
                $form->text('price')->required();
                $form->text('market_price')->default(0);
                $form->number('sales_fake')->default(rand(1,100));
                $form->number('stock')->default(9999);
                $form->multipleSelect('labels','标签')->options(Label::all()->pluck('name','id'))->customFormat(function ($v) {
                    if (! $v) {
                        return [];
                    }
                    // 从数据库中查出的二维数组中转化成ID
                    return array_column($v, 'id');
                });
                $form->editor('describe');
                $form->switch('status')->default(1);
                $form->hidden('sort')->default(0);

            })->tab('SKU', function (Form $form) {
                $form->text('spec_name');
                $form->hasMany('skus','SKU', function (Form\NestedForm $form) {
                    $form->text('name')->required();
                    $form->image('img')->autoUpload()->uniqueName()->move('product');
                    $form->number('stock','库存');

                });


            })->tab('属性', function (Form $form) {

                $form->hasMany('attribute','属性', function (Form\NestedForm $form) {
                    $form->text('name');
                    $form->textarea('content','内容');
                    //$form->switch('status')->default(1);
                });

            });




        });
    }
}
